package com.dongnam.quanly.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.dongnam.quanly.common.model.ChiTietHoaDon;
import com.dongnam.quanly.common.model.compkey.ChiTietHoaDonId;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class ChiTietHoaDonDao extends ExtendedGenericDao<ChiTietHoaDon, ChiTietHoaDonId> {
	public List<Object[]> getChiTietHoaDon(String sodonhang) {

		Query query = getSession().createQuery(
				"Select c.maVthh,sum(c.soLuong ),c.donGiaMua,c.hoaDon  from ChiTietHoaDon c , HoaDon h "
						+ "where c.hoaDon= h.soHoaDon and h.donHang=:sodonhang GROUP BY c.maVthh ORDER BY c.maVthh")
				.setParameter("sodonhang", sodonhang);

		@SuppressWarnings("unchecked")
		List<Object[]> list = query.list();
		return list;

	}

	@SuppressWarnings("unchecked")
	public List<ChiTietHoaDon> getListCthd(String hoadon) {

		String queryString = "Select cthd from ChiTietHoaDon cthd where cthd.hoaDon=:hoadon";
		Query query = getSession().createQuery(queryString);

		query.setParameter("hoadon", hoadon);

		List<ChiTietHoaDon> list = query.list();

		return list;

	}

	public boolean checkChiTiet(String hoadon, String maVthh) {

		String queryString = "Select cthd.hoaDon from ChiTietHoaDon cthd where cthd.hoaDon=:hoadon and cthd.maVthh=:maVthh";
		Query query = getSession().createQuery(queryString);

		query.setParameter("hoadon", hoadon);
		query.setParameter("maVthh", maVthh);
		boolean isCheck;
		if (query.list().size() > 0) {
			isCheck = true;
		} else {
			isCheck = false;
		}

		return isCheck;

	}
}
